import 'dart:convert';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/screenutil.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:shop/config/config.dart';
import 'package:shop/http/HttpUtil.dart';
import 'package:shop/route/my_route.dart';
import 'package:shop/shop/merchant/orders/OrderDetailPage.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:url_launcher/url_launcher.dart';

class GuoQiYuyue extends StatefulWidget {
  GuoQiYuyue({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _GuoQiYuyue createState() => _GuoQiYuyue();
}

class _GuoQiYuyue extends State<GuoQiYuyue> {

  bool isShow=true;
  SharedPreferences prefs;
  String merchantID=""; //商户ID
  List guoqiYuyue=[];   //过期预约集合
  int page = 1;         //页数

  // 上拉加载控制器
  RefreshController _refreshController = RefreshController(initialRefresh: false);
  void _onRefresh() async{
    _refreshController.refreshCompleted();
  }

  //初始化数据
  @override
  void initState() {
    super.initState();
    _onLoading();
  }

  // 加载商户已完成的订单
  _onLoading() async{
    prefs = await SharedPreferences.getInstance();
    merchantID = prefs.getString("merchantID");
    var x = await HttpUtil().get("/waimai/merchantYuyue/getXianYouandGuoQiYuyue?limit=10&page=${page}&merchantId="+merchantID+"&type=1");
    page++;
    if(x['code']==200){
      guoqiYuyue.addAll(x['data']);
      if(x['data'].length<1){
        _refreshController.loadNoData();
        return;
      }
    }
    if(mounted){
      setState(() {});
      _refreshController.loadComplete();
    }
  }

  @override
  Widget build(BuildContext context) {
    ScreenUtil.init(context , width: 464 , height: 820) ;
    return Scaffold(
        // body: ListView(
        //   children: [
        //     _getOrders(),
        //   ],
        // )
      body: Column(
        children: [
          Expanded(child: SmartRefresher(
            enablePullDown: false,
            enablePullUp: true,
            header: WaterDropHeader(),
            footer: CustomFooter(
              builder: (BuildContext context,LoadStatus mode){
                Widget body ;
                if(mode==LoadStatus.idle){
                  body =  Text("上拉加载");
                }
                else if(mode==LoadStatus.loading){
                  body =  CupertinoActivityIndicator();
                }
                else if(mode == LoadStatus.failed){
                  body = Text("加载失败！点击重试！");
                }
                else if(mode == LoadStatus.canLoading){
                  body = Text("松手,加载更多!");
                }
                else{
                  body = Text("没有更多数据了!");
                }
                return Container(
                  height: 55.0,
                  child: Center(child:body),
                );
              },
            ),
            controller: _refreshController,
            onRefresh: _onRefresh,
            onLoading: _onLoading,
            child: ListView.builder(
              itemBuilder: (c, i) => Card(child: _shouYuyue(guoqiYuyue[i])),
              itemExtent: 100.0,
              itemCount: guoqiYuyue.length,
            ),
          ),)
        ],
      ),
    );
  }

  GestureDetector _shouYuyue(item){
    return GestureDetector(
      child: Container(
        child: Column(
          children: [
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Text(item['nick'],style: TextStyle(fontWeight: FontWeight.bold,fontSize: 20,color: Colors.red),),
                Text("预留电话:"+item['phone'])
              ],
            ),
            SizedBox(height: 10,),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Text(item['mgoods']['gname']),
                Text(item['yuyueTime'],style: TextStyle(color: Colors.black12),)
              ],
            ),
            Row(
              children: [
                Text("人数："+item['num'].toString())
              ],
            )
          ],
        ),
      ),
      onTap: (){

      },
    );
  }

  //遍历已完成订单
  // _getOrders(){
  //   List<Widget> x = [];
  //   for(int i=0;i<guoqiYuyue.length;i++){
  //     x.add(
  //       Container(
  //         padding: EdgeInsets.all(10),
  //         margin: EdgeInsets.only(bottom: 20),
  //         color: Colors.white,
  //         child: Column(
  //           children: [
  //             Row(
  //               mainAxisAlignment: MainAxisAlignment.spaceBetween,
  //               children: [
  //                 Text("订单号："+orderList[i]['id']),
  //                 Text(orderList[i]['orTime'].toString().substring(0,10))
  //               ],
  //             ),
  //             SizedBox(height: 10,),
  //             _getOrderDetail(orderList[i]['morderDetailList']),
  //             Container(
  //               child: Column(
  //                 children: [
  //                   Row(children: [
  //                     Expanded(child: orderList[i]['orRemark']!=null ? Text("备注："+orderList[i]['orRemark']) : Text(""),),
  //                     Text("共"+orderList[i]['orTotalnum'].toString()+"件"),
  //                     Text("合计:￥"+orderList[i]['orPrice'].toString())
  //                   ],),
  //                   SizedBox(height: 5,),
  //                   // Row(children: [
  //                   //   Icon(Icons.place_outlined,color: Colors.orange,),
  //                   //   SizedBox(width: 10,),
  //                   //   // Expanded(child: Text("湖南省株洲市天元区泰山街道珠江北路2000号幸福大厦909未来传媒")),
  //                   //   Expanded(child: Text(orderList[i]['address']['addressDetail'])),
  //                   // ],),
  //                   Row(children: [
  //                     GestureDetector(child: Icon(Icons.phone,color:Colors.blueAccent,),onTap: () async {
  //                       await launch("tel:"+orderList[i]['address']['adPhone']);
  //                     },),
  //                     SizedBox(width: 10,),
  //                     Expanded(child: Text("电话:"+orderList[i]['address']['adPhone']+" "+orderList[i]['address']['adName'])),
  //                     GestureDetector(
  //                       child: Container(
  //                         padding: EdgeInsets.symmetric(vertical: 5,horizontal: 10),
  //                         decoration: BoxDecoration(
  //                           border: Border.all(color: Colors.redAccent , width: 1) ,
  //                           borderRadius: BorderRadius.all(Radius.circular(6)) ,
  //                         ),
  //                         child: Text("详情"),
  //                       ),
  //                       onTap: (){
  //                         print("点击订单详情了");
  //                         MyRoute.push(OrderDetailPage(orId: orderList[i]['id'],));
  //                       },
  //                     )
  //                   ],),
  //                 ],
  //               ),
  //             ),
  //           ],
  //         ),
  //       ),
  //     );
  //   }
  //   return Container(
  //     child: Column(children: x),
  //   );
  // }

  //得到订单商品
  // _getOrderDetail(orderDetailList){
  //   List<Widget> x = [];
  //   for(int i=0;i<orderDetailList.length;i++){
  //     x.add(
  //       Row(
  //         mainAxisAlignment: MainAxisAlignment.spaceBetween,
  //         children: [
  //           // Image.asset("assets/7.png",width: 90,height: 90,),
  //           Image.network(Config.getInstance().getHost()+orderDetailList[i]['mgoods']['gimages'],width: 90,height: 90,),
  //           SizedBox(width: 10,),
  //           Expanded(child: Column(
  //             crossAxisAlignment: CrossAxisAlignment.start,
  //             children: [
  //               Text(orderDetailList[i]['mgoods']['gname']),
  //               SizedBox(height: 5,),
  //               orderDetailList[i]['guiges']!=null ? Text(orderDetailList[i]['guiges'],style: TextStyle(color: Colors.black38),) : Text(""),
  //               // Text("*"+orderDetailList[i]['odNum'].toString())
  //             ],
  //           ),),
  //           Text("￥"+orderDetailList[i]['odCost'].toString()),
  //         ],
  //       ),
  //     );
  //   }
  //   return Container(
  //     child: Column(children: x),
  //   );
  // }

}